/*
 * NextAction.java
 *
 * Created on June 22, 2005, 8:33 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package com.bff.mpd.musicjukebox.lib.actions;

import com.bff.mpd.utils.Utils;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import org.bff.javampd.MPDPlayer;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDPlayerException;

/**
 *
 * @author Bill
 */
public class PlaylistLoopAction extends AbstractAction {

    private MPDPlayer player;
    private boolean looped;

    /** Creates a new instance of StopAction */
    public PlaylistLoopAction(MPDPlayer player) {
        super(Utils.getMessage("ACTION_LOOP_LOOPED_NAME"));
        this.player = player;
        putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("/resources/mpd/unloop_2.png")));
        putValue(Action.MNEMONIC_KEY, new Integer('L'));
        putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl L"));
        putValue(Action.SHORT_DESCRIPTION, Utils.getMessage("ACTION_LOOP_LOOPED_SHORT_DESC"));

        try {
            if (player.isRepeat()) {
                this.looped = true;
            } else {
                this.looped = false;
            }
            checkLooped();
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlaylistLoopAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlayerException ex) {
            Logger.getLogger(PlaylistLoopAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        try {
            player.setRepeat(!isLooped());
            checkLooped();
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlaylistLoopAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlayerException ex) {
            Logger.getLogger(PlaylistLoopAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void checkLooped() {
        if (isLooped()) {
            putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("/resources/mpd/unloop_2.png")));
            //putValue(Action.NAME, Utils.getMessage("ACTION_LOOP_UNLOOPED_NAME"));
            //putValue(Action.SHORT_DESCRIPTION, Utils.getMessage("ACTION_LOOP_LOOPED_SHORT_DESC"));
            setLooped(false);
        } else {
            putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("/resources/mpd/loop.png")));
            //putValue(Action.NAME, Utils.getMessage("ACTION_LOOP_LOOPED_NAME"));
            //putValue(Action.SHORT_DESCRIPTION, Utils.getMessage("ACTION_LOOP_UNLOOPED_SHORT_DESC"));
            setLooped(true);
        }
    }

    public boolean isLooped() {
        return looped;
    }

    public void setLooped(boolean looped) {
        this.looped = looped;
    }
}
